//
// Created by entaoyang@163.com on 2017/10/25.
// Copyright (c) 2017 yet.net. All rights reserved.
//

import Foundation
import CoreGraphics

public enum Conflict: String {
    case abort = "ABORT" //default
    case replace = "REPLACE"
    case rollback = "ROLLBACK"
    case fail = "FAIL"
    case ignore = "IGNORE"
}

public typealias ColumnName = String

public extension ColumnName {
    var toName: String {
        self
    }
}


public extension KeyValue {
    var sqlValue: SQLValue {
        value as! SQLValue
    }
}

public extension KeyAny {
    var sqlValue: SQLValue {
        if self.value == nil {
            return self.value as! SQLValue
        } else {
            return SQLNull.inst
        }
    }
}

public typealias RowData = [String: SQLValue]


public protocol SQLValue {
}


public class SQLNull: SQLValue, ExpressibleByNilLiteral {
    public static var inst: SQLNull = SQLNull()

    private init() {
    }

    public required init(nilLiteral: ()) {
    }
}

public protocol SQLText: SQLValue {
    var toSQLText: String { get }
}

public protocol SQLInt: SQLValue {
    var toSQLInt: Int64 { get }
}

public protocol SQLReal: SQLValue {
    var toSQLReal: Double { get }
}

public protocol SQLBlob: SQLValue {
    var toSQLBlob: Data { get }
}

extension Bool: SQLInt {
    public var toSQLInt: Int64 {
        self ? 1 : 0
    }
}

extension Int8: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension UInt8: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension Int16: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension UInt16: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension Int32: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension UInt32: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension Int: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension UInt: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension Int64: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension UInt64: SQLInt {
    public var toSQLInt: Int64 {
        Int64(self)
    }
}

extension Float: SQLReal {
    public var toSQLReal: Double {
        Double(self)
    }
}

extension Double: SQLReal {
    public var toSQLReal: Double {
        self
    }
}

extension CGFloat: SQLReal {
    public var toSQLReal: Double {
        Double(self)
    }
}

extension Character: SQLText {
    public var toSQLText: String {
        String(self)
    }
}

extension String: SQLText {
    public var toSQLText: String {
        self
    }
}

extension NSString: SQLText {
    public var toSQLText: String {
        String(self)
    }
}

extension Data: SQLBlob {
    public var toSQLBlob: Data {
        self
    }
}

extension NSData: SQLBlob {
    public var toSQLBlob: Data {
        Data(referencing: self)
    }
}



